<?php

namespace bc\model;

use bc\ar\MActiveRecord;

class BindModel extends MActiveRecord
{
	
	/**
	 * 主键id
	 * @var unknown
	 */
	public $autoId = 'bind_id';
	
	/**
	 * 定义数据表
	 */
	public static function tableName()
	{
		return '{{%order_bind}}'; 
	}
	
	/**
	 * 
	 * @param string $params
	 * @param int $page
	 * @param int $pageSize
	 */
	public function getReportList($params, $page = null, $pageSize = null)
	{
		//select o.goods_name, b.* from bc_order as o left join bc_order_bind as b on o.id = b.order_id
			//$ar = $this->find ()->select ( 'bc_order.order_no, bc_order_bind.*,bc_item_data.*' )
				$ar = $this->find ()->select ( 'bc_order.order_no,bc_order_bind.bind_id as bid_id ,bc_order_bind.*,bc_item_data1.item_value as name,(case bc_item_data2.item_value when 30 then "男" when 31 then "女" else "" end ) as sex,bc_item_data3.item_value as birthday,bc_express.tracking_no,bc_express_company.company_name' )
		->leftJoin('bc_order', 'bc_order.order_id = bc_order_bind.order_id')
					->leftJoin('bc_item_data as bc_item_data1','bc_item_data1.bind_id = bc_order_bind.bind_id and bc_item_data1.item_product_id=1')
					->leftJoin('bc_item_data as bc_item_data2','bc_item_data2.bind_id = bc_order_bind.bind_id and bc_item_data2.item_product_id=2')
			->leftJoin('bc_item_data as bc_item_data3','bc_item_data3.bind_id = bc_order_bind.bind_id and bc_item_data3.item_product_id=3')
					->leftJoin('bc_express', 'bc_express.bind_id = bc_order_bind.bind_id')
					->leftJoin('bc_express_company', 'bc_express_company.company_id = bc_express.company_id');
				//->having('`bc_item_data`.`item_product_id` is null or  `bc_item_data`.`item_product_id` in (1,2,3) ');

				//select bc_order.order_no,bc_order_bind.*,bc_item_data.* from bc_order right join bc_order_bind on bc_order_bind.order_id = bc_order.order_id left join bc_item_data on bc_order_bind.user_id = bc_item_data.user_id;
		if ($params)
		{
			$ar->where ( $params );
		}
		if ($page && $pageSize)
		{
			$offset = $this->getOffset ( $page, $pageSize );
			$ar->offset ( $offset )->limit ( $pageSize );
		}

//		echo $ar->createCommand()->getRawSql();
                
		if ($this->sort != null)
			$ar->orderBy ( $this->sort );
		return $ar->asArray ()->all ();
	}
        
	public function getTotal($params='1=1')
	{
		if ($params) {
			return $this->find()
				->innerJoin('bc_order', 'bc_order.order_id = bc_order_bind.order_id')
				->leftJoin('bc_express', 'bc_express.bind_id = bc_order_bind.bind_id')
				->where ( $params )->count();
		}else{
			return $this->find()
				->innerJoin('bc_order', 'bc_order.order_id = bc_order_bind.order_id')
				->leftJoin('bc_express', 'bc_express.bind_id = bc_order_bind.bind_id')
				->count();
		}
	}

	//根据bind_id获取订单信息
	public function getOrderInfoByBindid($bind_id)
	{
		$ar =  $this->find()->innerJoin('bc_order', 'bc_order.order_id = bc_order_bind.order_id')->where ( ['bc_order_bind.bind_id'=>$bind_id] );
		return $ar->asArray ()->one();
	}

	//根据ID 查询订单详细信息
	public function getSampleInfo($bind_id)
	{
//		$ar = $this->find ()->select ( 'bc_order.order_no, bc_order_bind.bind_id, bc_order_bind.order_id, bc_order_bind.bind_no, bc_order_bind.user_id, bc_order_bind.bind_status, bc_order_bind.name, bc_order_bind.sex, bc_order_bind.mobile, bc_order_bind.create_time, bc_item_data.id, bc_item_data.item_value, bc_item_data.item_product_id, bc_item_data.deleted')
//			->leftJoin('bc_order', 'bc_order.order_id = bc_order_bind.order_id')
//			->leftJoin('bc_item_data', 'bc_order_bind.bind_id=bc_item_data.bind_id');
//		if ($bind_id)
//		{
//			$ar->where ( ['bc_order_bind.bind_id'=>$bind_id] );
//		}
//		echo $ar->createCommand()->sql;
//		$data = $ar->asArray()->all();

		$sql = "SELECT `bc_order`.`order_no`,`bc_order`.`send_user`, `bc_order`.`send_time`, `bc_order_bind`.`bind_id`, `bc_order_bind`.`order_id`, `bc_order_bind`.`bind_no`, `bc_order_bind`.`user_id`, `bc_order_bind`.`bind_status`, `bc_order_bind`.`name`, `bc_order_bind`.`sex`, `bc_order_bind`.`mobile`, `bc_order_bind`.`create_time`, `bc_item_data`.`id`, `bc_item_data`.`item_value`, `bc_item_data`.`item_product_id`, `bc_item_data`.`deleted` FROM `bc_order_bind` LEFT JOIN `bc_order` ON bc_order.order_id = bc_order_bind.order_id LEFT JOIN `bc_item_data` ON bc_order_bind.bind_id = bc_item_data.bind_id WHERE `bc_order_bind`.`bind_id` = $bind_id";

		$data = BindModel::findBySql( $sql )->asArray()->one();
		return $data;
	}
}